'use strict';

const Controller = require('egg').Controller;

const formateTree = treedata =>{
  return treedata.map(el=>{
    let obj = {
      id:el.menuId,
      label:el.menuName
    };
    if(el.children instanceof Array) {
      obj.children = formateTree(el.children);
    }
    return obj
  })
}

class MenuController extends Controller {
  async index() {
    const {ctx,app} = this;
    const query = ctx.query;
    const {Op} = app.Sequelize;
    let params = {
      where:{},
      order:app.Sequelize.col("orderNum")
    }
    if(query.menuName){
      params.where.menuName = {[Op.like] : "%"+query.menuName+"%"}
    }
    if(query.status){
      params.where.status = query.status;
    }
    let result = await ctx.service.menu.findAll(params);
    ctx.body =  ctx.helper.formateResponse(result);
  }
  async show(){
    const {ctx} = this;
    const {id} = ctx.params;
    let menu = await ctx.model.SysMenu.findByPk(id);
    ctx.body = ctx.helper.formateResponse(menu);
  }
  async update(){
    const {ctx} = this;
    const newV = ctx.request.body;
    const result = await ctx.model.SysMenu.findByPk(newV.menuId);
    const user = ctx.session.userinfo;
    let updateData = {
      parentId:newV.parentId,
      menuType:newV.menuType,
      menuName:newV.menuName,
      icon:newV.icon,
      orderNum:newV.orderNum,
      isFrame:newV.isFrame,
      isCache:newV.isCache,
      status:newV.status,
      component:newV.component,
      path:newV.path,
      perms:newV.perms,
      visible:newV.visible,
      updateBy:user.userName
    };
    await result.update(updateData);
    ctx.body = ctx.helper.formateResponse(null);
  }
  async create(){
    const {ctx} = this;
    const user = ctx.session.userinfo;
    const newV = ctx.request.body;
    let updateData = {
      parentId:newV.parentId,
      menuType:newV.menuType,
      menuName:newV.menuName,
      icon:newV.icon,
      orderNum:newV.orderNum,
      isFrame:newV.isFrame,
      isCache:newV.isCache,
      status:newV.status,
      component:newV.component,
      path:newV.path,
      perms:newV.perms,
      visible:newV.visible,
      createBy:user.userName
    };
    try {
      const result = await ctx.model.SysMenu.create(updateData);
      ctx.body = ctx.helper.formateResponse(result);
    } catch (error) {
      ctx.body = ctx.helper.formateResponse(null,500,'操作失败')
    }
  }
  async getMenuByRole(){
    const {ctx} = this;
    const {id} = ctx.params;
    const role = await ctx.model.SysRole.findOne({
      where:{
        roleId:Number(id)
      },
      include:[ctx.model.SysMenu]
    });
    const checkedKeys = role.sys_menus.map(el=>el.toJSON()).map(el=>el.menuId);
    let menus = await ctx.model.SysMenu.findAll();
    menus = menus.map(el=>el.toJSON());
    const treedata = await ctx.helper.handleTree(menus,"menuId","parentId");
    ctx.body = {
      code:200,
      msg:"操作成功",
      menus:formateTree(treedata),
      checkedKeys
    };
  }
  async treeselect(){
    const ctx = this.ctx;
    let menus = await ctx.model.SysMenu.findAll();
    menus = menus.map(el=>el.toJSON());
    const treedata = await ctx.helper.handleTree(menus,"menuId","parentId");
    ctx.body = ctx.helper.formateResponse(formateTree(treedata));
  }
  async destroy(){
    // menuId
    const {ctx,app} = this;
    const {id} = ctx.params;
    const {Op} = app.Sequelize;
    await ctx.model.SysMenu.destroy({
      where:{
        [Op.or]:[
          {menuId:Number(id)},
          {parentId:Number(id)}
        ]
      }
    });
    ctx.body = ctx.helper.formateResponse(null);
  }
}

module.exports = MenuController;
